<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="c端用户主键" prop="pbUserId">
      <el-input v-model="dataForm.pbUserId" placeholder="c端用户主键"></el-input>
    </el-form-item>
    <el-form-item label="宠物类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="宠物类型"></el-input>
    </el-form-item>
    <el-form-item label="宠物名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="宠物名称"></el-input>
    </el-form-item>
    <el-form-item label="宠物等级" prop="petLevel">
      <el-input v-model="dataForm.petLevel" placeholder="宠物等级"></el-input>
    </el-form-item>
    <el-form-item label="宠物经验" prop="petExp">
      <el-input v-model="dataForm.petExp" placeholder="宠物经验"></el-input>
    </el-form-item>
    <el-form-item label="闯关等级" prop="roundLevel">
      <el-input v-model="dataForm.roundLevel" placeholder="闯关等级"></el-input>
    </el-form-item>
    <el-form-item label="积分(金币)" prop="integral">
      <el-input v-model="dataForm.integral" placeholder="积分(金币)"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          pbUserId: '',
          type: '',
          name: '',
          petLevel: '',
          petExp: '',
          roundLevel: '',
          integral: ''
        },
        dataRule: {
          pbUserId: [
            { required: true, message: 'c端用户主键不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '宠物类型不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '宠物名称不能为空', trigger: 'blur' }
          ],
          petLevel: [
            { required: true, message: '宠物等级不能为空', trigger: 'blur' }
          ],
          petExp: [
            { required: true, message: '宠物经验不能为空', trigger: 'blur' }
          ],
          roundLevel: [
            { required: true, message: '闯关等级不能为空', trigger: 'blur' }
          ],
          integral: [
            { required: true, message: '积分(金币)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbpet/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.data.pbPet
                // this.dataForm.pbUserId = data.pbpet.pbUserId
                // this.dataForm.type = data.pbpet.type
                // this.dataForm.name = data.pbpet.name
                // this.dataForm.petLevel = data.pbpet.petLevel
                // this.dataForm.petExp = data.pbpet.petExp
                // this.dataForm.roundLevel = data.pbpet.roundLevel
                // this.dataForm.integral = data.pbpet.integral
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbpet/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                pbPet: this.dataForm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  // duration: 1500,
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
